<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\common\Util;
use support\exception\BusinessException;

/**
 * 自动续费服务类
 **/
class RenewAutoService
{

    /**
     * 自动续费
     *
     * @param array $params
     **/
    public static function index($params)
    {
        list($id, $data) = array_values($params);

        $config = OptionService::getConfig('order');
        if (!$config['order_node_renew_time']) {
            throw new BusinessException('到期前几天自动续费配置错误！');
        }
        if (!$config['order_node_renew_duration']) {
            throw new BusinessException('续费时间配置错误！');
        }

        $user = (array)Util::db()->table('wa_admins')
            ->where('id', $data['user_id'])
            ->select('id','username','money','user_status')
            ->first();
        if (!$user) {
            throw new BusinessException('会员不存在或已删除！');
        }
        $params = ['line_id' => $data['line_id'], 'duration' => $config['order_node_renew_duration'], 'ids' => [$id]];
        OrderNodeService::renew($params, $user,$user, 'renew_auto_queue');
        return true;
    }

}
